-- UIBuildingView4
-- Created by chengb Aug/13/2016
-- 建筑查看界面（含4个按钮）

UIBuildingView4 = class("UIBuildingView4", function()
    return cc.Layer:create();
end);

function UIBuildingView4.create(gridData, okFunc1, okFunc2, okFunc3, okFunc4, okText1, okText2, okText3, okText4, dialog, title, iconPath)
    return UIBuildingView4.new(gridData, okFunc1, okFunc2, okFunc3, okFunc4, okText1, okText2, okText3, okText4, dialog, title, iconPath);
end

-- 按钮数量
local MAX_BUTTON_NUM = 4;

-- 构造函数
function UIBuildingView4:ctor(gridData, okFunc1, okFunc2, okFunc3, okFunc4, okText1, okText2, okText3, okText4, dialog, title, iconPath)
    -- 初始化
    self:setName("UIBuildingView4");
    local node = cc.CSLoader:createNode("layout/dungeon/BuildingView4.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    assert(type(gridData) == "table", "必须传入格子数据");

    self.gridData  = gridData;

    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class) or {};

    local cfgOffsetY = buildingInfo["icon_offset_y"];
    if type(cfgOffsetY) ~= "number" then
        cfgOffsetY = 0;
    end

    self.title         = title or buildingInfo["title"];                               -- 标题
    self.iconPath      = iconPath or getLevelItemIconPath(buildingInfo["icon"]);       -- 图标路径
    self.dialog        = dialog or BuildingBonusM.getBuildingDialog(self.gridData);    -- 对话内容
    self.iconOffsetY   = cfgOffsetY;                                                   -- 图标纵向偏移量

    self.buttonMap = {
        { ["text"] = okText1, ["callback"] = okFunc1, },
        { ["text"] = okText2, ["callback"] = okFunc2, },
        { ["text"] = okText3, ["callback"] = okFunc3, },
        { ["text"] = okText4, ["callback"] = okFunc4, },
    }

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBuildingView4:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIBuildingView4:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBuildingView4 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIBuildingView4:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBuildingView4");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册4个按钮点击事件
    for i = 1, MAX_BUTTON_NUM do
        local btn = findChildByName(node, string.format("CT/btn%d", i));
        local text     = self.buttonMap[i].text;
        local callback = self.buttonMap[i].callback;

        TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btn:setTitleText(text);

        local function onButtonClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                AudioM.playFx("button_select");
                if type(callback) == 'function' then
                    callback();
                end

                -- 关闭当前界面
                UIMgr.getCurrentScene():removeFormByName("UIBuildingView4");
            end
        end
        AddTouchEventListener(btn, onButtonClick);
    end
end

-- 重绘
function UIBuildingView4:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");

    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    -- 图标
    iconImg:loadTexture(self.iconPath);
    iconImg:setPositionY(iconImg:getPositionY() + self.iconOffsetY);
    iconImg:setScale(1.3);

    -- 标题
    titleLabel:setString(self.title);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    -- 描述
    applyString(descLabel, self.dialog);
end










